package com.booth.base.services.impl;

import java.util.ArrayList;
import java.util.List;

import com.booth.base.model.DataDict;
import com.booth.base.services.IDataDictService;
/***
 * 数据字典业务逻辑控制器
 * @TypeName：DataDictService
 * @description：
 * @author：Administrator
 */
public class DataDictService implements IDataDictService {
	private List<DataDict> lstAll;
	
	public List<DataDict> showDataDictTree(Short status) {
		if (status == null) {
			this.lstAll = DataDict.dao.find("select * from data_dict");
		} else {
			this.lstAll = DataDict.dao
					.find("select id,dat_id,name as text from data_dict where status=?",
							new Object[] { status });
		}
		List lstTree = findById(null);
		return lstTree;
	}

	private List<DataDict> findById(Integer parentId) {
		List subs = new ArrayList();
		for (int i = 0; i < this.lstAll.size(); i++) {
			DataDict mod = (DataDict) this.lstAll.get(i);
			if (parentId == null) {
				if ((mod.get("dat_id") != null)
						&& (((Integer) mod.get("dat_id")).intValue() != 0))
					continue;
				this.lstAll.remove(i);
				i--;
				List modSubs = findById(mod.getInt("id"));
				if (modSubs.size() > 0) {
					mod.put("children", modSubs);
				}
				subs.add(mod);
			} else {
				if ((mod.get("dat_id") == null)
						|| (((Integer) mod.get("dat_id")).intValue() != parentId
								.intValue()))
					continue;
				List modSubs = findById(mod.getInt("id"));
				if (modSubs.size() > 0) {
					mod.put("children", modSubs);
				}
				subs.add(mod);
			}
		}

		return subs;
	}

	public List<DataDict> showDataDictByCode(String code) {
		return DataDict.dao
				.find("select id,name from data_dict where status=9 and dat_id in (select id from data_dict where code=?) order by show_order asc",
						new Object[] { code });
	}
}